package com.examine.serviceImpl;

import com.examine.bean.User;
import com.examine.dao.UserMapper;
import com.examine.service.UserService;
import com.examine.utils.DataConfig;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    private static SqlSession session = null;
    private static UserMapper userMapper = null;
    private User user = null;
    private List<User> userList = null;
    private int count = 0;

    public static void dataConfig(){
        try{
            session = DataConfig.getSession();
            userMapper = session.getMapper(UserMapper.class);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public User getUserLogin(User u) {
        dataConfig();
        try{
            user = userMapper.getUserLogin(u);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return user;
    }


    @Override
    public User selectByPrimaryKey(String id) {
        dataConfig();

        try{
            user = userMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return user;
    }

    @Override
    public List<User> getAllUserList(User record) {
        dataConfig();
        try{
            userList = userMapper.getAllUserList(record);
            for(User p : userList){

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return userList;
    }


    @Override
    public int deleteByPrimaryKey(String id) {
        dataConfig();

        try{
            count = userMapper.deleteByPrimaryKey(id);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int insertSelective(User u) {
        dataConfig();

        try{
            count = userMapper.insertSelective(u);

            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int updateByPrimaryKeySelective(User u) {
        dataConfig();
        try{
            count = userMapper.updateByPrimaryKeySelective(u);
            if (count > 0){
                session.commit();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int queryCountLike(User u) {
        dataConfig();

        try{
            count = userMapper.queryCountLike(u);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }
}
